package com.ksyzt.ksite2.client.news;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;
import com.ksyzt.gwt.client.common.MessageComposite;
import com.ksyzt.gwt.client.event.MessageEvent;
import com.ksyzt.gwt.client.event.MessageHandler;
import com.ksyzt.gwt.client.mainframe.AppData;
import com.ksyzt.gwt.client.mainframe.IAppModule;
import com.ksyzt.gwt.client.mainframe.ModuleProperties;
import com.ksyzt.ksite2.client.column.ColumnTree;
import com.ksyzt.ksite2.client.column.ColumnType;
import com.ksyzt.ksite2.client.common.ServerProxy;
import com.ksyzt.ksite2.shared.OrgType;
import com.ksyzt.ksite2.shared.PageNewsData;
import com.ksyzt.ksite2.shared.modle.COLUMNObj;
import com.ksyzt.ksite2.shared.modle.VW_NEWSObj;

public class NewsManager extends MessageComposite implements IAppModule {

	private static PageNewsExplorerUiBinder uiBinder = GWT
			.create(PageNewsExplorerUiBinder.class);

	interface PageNewsExplorerUiBinder extends UiBinder<Widget, NewsManager> {
	}

	public NewsManager() {
		initWidget(uiBinder.createAndBindUi(this));
		treeColumn.addMessageHandler(m_tree_handler);
		treeColumn.EnableCreate(false);
	}

	COLUMNObj m_column;
	protected AsyncCallback<PageNewsData> m_on_article_handler = new AsyncCallback<PageNewsData>() {

		@Override
		public void onSuccess(PageNewsData result) {
			if (result.news != null && result.news.size() > 0) {
				VW_NEWSObj news = result.news.get(0);
				sureEditor();
				m_editor.edit(news, m_column.getID());
				m_editor.enableReturn(false);
			} else {
				sureEditor();
				m_editor.edit(null, m_column.getID());
				m_editor.enableReturn(false);
			}
			message("操作成功!");
		}

		@Override
		public void onFailure(Throwable caught) {
			message(caught.getMessage());
		}
	};
	MessageHandler m_tree_handler = new MessageHandler() {

		public void onMessage(Object sender, Integer message, Object value) {
			TreeItem ti = (TreeItem) value;
			m_column = (COLUMNObj) ti.getUserObject();
			switch (m_column.getCOLUMNTYPE()) {
			case ColumnType.CT_PICTURE: {
				// 图片列表
				surePictureList();
				m_pictureList.initList(m_column);
				break;
			}
			case ColumnType.CT_ARTICLE: {
				// 单篇文章直接编辑
				ServerProxy.SYS_MANAGER.getNews(m_column.getID(), 0, 1,
						m_on_article_handler);
				break;
			}
			default: {
				sureList();
				m_list.initList(m_column);
			}
			}

		}
	};
	private MessageHandler m_picture_handler = new MessageHandler() {

		@Override
		public void onMessage(Object sender, Integer message, Object value) {
			if (MessageEvent.ITEMCLICK.equals(message)) {

			} else if (MessageEvent.REFRESH.equals(message)) {
				refreshData();
			}
		}
	};

	public void refreshData() {
		initData(m_userid);
	}

	protected void surePictureList() {

		if (m_pictureList == null) {
			m_pictureList = new PictureList();
			m_pictureList.setUserId(m_userid);
			m_pictureList.addMessageHandler(m_picture_handler);
		}
		if (!m_pictureList.isAttached()) {
			if (current != null) {
				current.removeFromParent();
			}

			dockroot.add(m_pictureList);
			current = m_pictureList;
		}
	}

	PictureList m_pictureList;

	@UiField
	ColumnTree treeColumn;

	@UiField
	DockLayoutPanel dockroot;

	NewsList m_list;
	NewsEditor m_editor;

	int m_userid;
	private MessageHandler m_list_handler = new MessageHandler() {

		public void onMessage(Object sender, Integer message, Object value) {
			if (MessageEvent.ITEMCLICK.equals(message)) {
				VW_NEWSObj n = (VW_NEWSObj) value;
				sureEditor();
				m_editor.edit(n, m_column.getID());
				m_editor.enableReturn(true);
			} else if (MessageEvent.REFRESH.equals(message)) {
				refreshData();
			}
		}
	};
	private MessageHandler m_editor_handler = new MessageHandler() {

		public void onMessage(Object sender, Integer message, Object value) {

			if (MessageEvent.RETURN == message) {
				sureList();
				TreeItem ti = treeColumn.getSelectedItem();
				if (ti != null) {
					COLUMNObj column = (COLUMNObj) ti.getUserObject();
					m_list.initList(column);
				}
			} else if (MessageEvent.MESSAGE == message) {
				message((String) value);
			}
		}
	};

	Widget current;

	private void sureList() {
		if (m_list == null) {
			m_list = new NewsList();
			m_list.setUserId(m_userid);
			m_list.addMessageHandler(m_list_handler);
		}
		if (!m_list.isAttached()) {
			if (current != null) {
				current.removeFromParent();
			}

			dockroot.add(m_list);
			current = m_list;
		}
	}

	private void sureEditor() {
		if (m_editor == null) {
			m_editor = new NewsEditor();
			m_editor.setUserId(m_userid);
			m_editor.addMessageHandler(m_editor_handler);
		}
		if (!m_editor.isAttached()) {
			if (current != null) {
				current.removeFromParent();
			}

			dockroot.add(m_editor);
			current = m_editor;
		}
	}

	public void initData(int mUserid) {
		m_userid = mUserid;
		treeColumn.initTree(mUserid, OrgType.OT_NEWS,
				OrgType.getOrgNameByType(OrgType.OT_NEWS), false);
	}

	HandlerRegistration handler;

	@Override
	public void onInitialize(AppData application, ModuleProperties pros) {
		handler = this.addMessageHandler(application.messageHandler);

		initData(application.user.id);
	}

	@Override
	public void onUnInitialize() {
		handler.removeHandler();
	}

	@Override
	public Widget getRootWidget() {
		return this;
	}
}
